package com.sky.service.impl;

import cn.hutool.core.util.StrUtil;
import com.sky.service.ShopService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.sky.constant.RedisConstant.SHOP_STATUS_KEY;

@Service
@RequiredArgsConstructor
public class ShopServiceImpl implements ShopService {
    private final StringRedisTemplate redisTemplate;

    /**
     * 设置营业状态
     *
     * @param status
     */
    @Override
    public void setStatus(Integer status) {
        redisTemplate.opsForValue().set(SHOP_STATUS_KEY, status.toString());
    }

    /**
     * 获取营业状态
     *
     * @return
     */
    @Override
    public Integer getStatus() {
        String status = redisTemplate.opsForValue().get(SHOP_STATUS_KEY);
        // 如果Redis中没有设置过营业状态，则默认为0（即营业）
        if (StrUtil.isEmpty(status)) {
            redisTemplate.opsForValue().set(SHOP_STATUS_KEY, "1");
            status = redisTemplate.opsForValue().get(SHOP_STATUS_KEY);
        }
        return Integer.valueOf(status);
    }
}
